﻿using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.OS;
using Android.Widget;
using Android.Net;
using System;

namespace MWC.Android.Screens {
    [Activity(Label = "Home", ScreenOrientation = ScreenOrientation.Portrait)]
    public class HomeScreen : BaseScreen {
        protected MWC.Adapters.DaysListAdapter dayList;
        protected ListView dayListView = null;
        
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            // set our layout to be the home screen
            SetContentView(Resource.Layout.HomeScreen);

            //Find our controls
            dayListView = FindViewById<ListView>(Resource.Id.DayList);

            // wire up task click handler
            if (dayListView != null) {
                dayListView.ItemClick += (object sender, AdapterView.ItemClickEventArgs e) => {
                    
                    if(DaysManager.IsSpecificDayNews(e.Position))
                    {
                        bool internetConnection = AndroidUtility.isInternetConnection((ConnectivityManager)GetSystemService(ConnectivityService));
                        bool agendaParseError = false;
                        if ( internetConnection == true ) {
                            //jest internet przenies do nowej aktywnosci
                            NetcampWebProxy.RssItem agenda = DL.NetcampMeetings.getNewestAgendaInfo();
                            //sprawdz czy dobrze sparsowalo
                            if (agenda.Description != null)
                            {
                                var agendaDetails = new Intent(this, typeof(AgendaDetailsScreen));
                                agendaDetails.PutExtra("Description", agenda.Description);
                                StartActivity(agendaDetails);
                            }
                            else
                            {
                                agendaParseError = true;
                            }
                        }

                        if( agendaParseError == true || internetConnection == false  )
                        {
                            //nie ma internetu lub zle sparsowalo wyswietl alerta
                            AlertDialog.Builder builder = new AlertDialog.Builder(this);
                            AlertDialog alertDialog = builder.Create();
                            alertDialog.SetTitle(Resource.String.NoInternetAlertTitle);
                            alertDialog.SetMessage(Resources.GetString(Resource.String.NoInternetAlertMessage));
                            alertDialog.SetButton(Resources.GetString(Resource.String.NoInternetAlertButtonText), (s, ev) =>
                            {
                                //ewentualna logika kodu po nacisnieciu
                            });
                            alertDialog.Show();
                        }
                        
                    }
                    else
                    {
                        //wyswietl alerta jesli nie ma newsa na temat wydarzenia
                        AlertDialog.Builder builder = new AlertDialog.Builder(this);
                        AlertDialog alertDialog = builder.Create();
                        alertDialog.SetTitle(Resource.String.NoDayNewsAlertTitle);
                        alertDialog.SetMessage(Resources.GetString(Resource.String.NoDayNewsAlertMessage));
                        alertDialog.SetButton(Resources.GetString(Resource.String.NoDayNewsAlertButtonText), (s, ev) => 
                        {
                            //ewentualna logika kodu po nacisnieciu
                        });
                        alertDialog.Show();
                    }
                };
            }
        }

        protected override void OnResume()
        {
            base.OnResume();

            // create our adapter
            dayList = new MWC.Adapters.DaysListAdapter(this);

            //Hook up our adapter to our ListView
            dayListView.Adapter = this.dayList;
        }
    }
}
